package com.example.travel.controller;

import com.example.travel.bean.LineBean;
import com.example.travel.service.LineService;
import com.example.travel.utils.DateFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import java.util.List;
import java.util.Map;

@Controller
public class LineController {

    @Autowired
    LineService lineService;

    @RequestMapping("/getLineList")
    public ModelAndView getLineList(@RequestParam(required = false) String searchKeyword,
                                   @RequestParam(defaultValue = "1") int page,
                                   @RequestParam(defaultValue = "10") int pageSize) {
        ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());
        
        // 计算偏移量
        int offset = (page - 1) * pageSize;
        
        // 获取分页数据和总数
        List<LineBean> dataList = lineService.getLineListWithPagination(searchKeyword, offset, pageSize);
        int totalCount = lineService.getLineCount(searchKeyword);
        
        modelAndView.addObject("dataList", dataList);
        modelAndView.addObject("totalCount", totalCount);
        modelAndView.addObject("currentPage", page);
        modelAndView.addObject("pageSize", pageSize);
        modelAndView.addObject("status", "success");
        return modelAndView;
    }

    @RequestMapping(value="/deleteLine", method=RequestMethod.POST)
    public ModelAndView deleteLine(@RequestParam Map<String, Object> params) {
        ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());
        int lineID = Integer.parseInt(params.get("lineID").toString());
        int status = lineService.deleteLine(lineID);
        List<LineBean> dataList = lineService.getLineList();
        modelAndView.addObject("dataList", dataList);
        modelAndView.addObject("status", "success");
        return modelAndView;
    }

    @RequestMapping(value="/addLine", method=RequestMethod.POST)
    public ModelAndView addLine(@RequestParam Map<String, Object> params) {
        ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());

        String lineStart = params.get("lineStart").toString();
        String lineEnd = params.get("lineEnd").toString();
        String lineTimes = params.get("lineTimes").toString();
        int linePrice = Integer.parseInt(params.get("linePrice").toString());
        String lineType = params.get("lineType").toString();
        String lineDesc = params.get("lineDesc").toString();
        String lineAuthor = params.get("lineAuthor").toString();
        String linePhone = params.get("linePhone").toString();
        String linePicture = params.get("linePicture").toString();
        String lineStar = params.get("lineStar").toString();

        DateFormatter dateFormatter = new DateFormatter();
        String lineDate = dateFormatter.getDate();

        // 设置默认图片
        linePicture = linePicture.isEmpty() ? "https://img.zcool.cn/community/01f5ce56e20c1f32f875520fb0b199.jpg" : linePicture;

        LineBean lineBean = new LineBean();
        lineBean.setLineStart(lineStart);
        lineBean.setLineEnd(lineEnd);
        lineBean.setLineTimes(lineTimes);
        lineBean.setLinePrice(linePrice);
        lineBean.setLineType(lineType);
        lineBean.setLineDesc(lineDesc);
        lineBean.setLineAuthor(lineAuthor);
        lineBean.setLinePhone(linePhone);
        lineBean.setLinePicture(linePicture);
        lineBean.setLineDate(lineDate);
        lineBean.setLineStatus("normal");
        lineBean.setLineStar(lineStar); // 默认5星

        int status = lineService.addLine(lineBean);
        List<LineBean> dataList = lineService.getLineList();
        modelAndView.addObject("dataList", dataList);
        modelAndView.addObject("status", "success");
        return modelAndView;
    }

    @RequestMapping(value="/updateLine", method=RequestMethod.POST)
    public ModelAndView updateLine(@RequestParam Map<String, Object> params) {
        ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());

        int lineID = Integer.parseInt(params.get("lineID").toString());
        String lineStart = params.get("lineStart").toString();
        String lineEnd = params.get("lineEnd").toString();
        String lineTimes = params.get("lineTimes").toString();
        int linePrice = Integer.parseInt(params.get("linePrice").toString());
        String lineType = params.get("lineType").toString();
        String lineDesc = params.get("lineDesc").toString();
        String lineAuthor = params.get("lineAuthor").toString();
        String linePhone = params.get("linePhone").toString();
        //String linePicture = params.get("linePicture").toString();
        String lineStar = params.get("lineStar").toString();

        DateFormatter dateFormatter = new DateFormatter();
        String lineDate = dateFormatter.getDate();

        LineBean lineBean = new LineBean();
        lineBean.setLineID(lineID);
        lineBean.setLineStart(lineStart);
        lineBean.setLineEnd(lineEnd);
        lineBean.setLineTimes(lineTimes);
        lineBean.setLinePrice(linePrice);
        lineBean.setLineType(lineType);
        lineBean.setLineDesc(lineDesc);
        lineBean.setLineAuthor(lineAuthor);
        lineBean.setLinePhone(linePhone);
        //lineBean.setLinePicture(linePicture);
        lineBean.setLineStar(lineStar);
        lineBean.setLineStatus("normal");
        lineBean.setLineDate(lineDate);

        int status = lineService.updateLine(lineBean);
        List<LineBean> dataList = lineService.getLineList();
        modelAndView.addObject("dataList", dataList);
        modelAndView.addObject("status", "success");
        return modelAndView;
    }



    @RequestMapping(value="/getLastLines", method=RequestMethod.GET)
    public ModelAndView getLastLines() {
        ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());
        List<LineBean> lines = lineService.getLastLines();
        modelAndView.addObject("dataList", lines );
        modelAndView.addObject("status", "success");
        return modelAndView;
    }
}
